package com.bootdo.system.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.system.domain.DlPositionClassDO;
import com.bootdo.system.service.DlPositionClassService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.system.domain.DlPositionSubclassDO;
import com.bootdo.system.service.DlPositionSubclassService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2021-06-07 09:22:54
 */
 
@Controller
@RequestMapping("/system/dlPositionSubclass")
public class DlPositionSubclassController {
	@Autowired
	private DlPositionSubclassService dlPositionSubclassService;
	@Autowired
	private DlPositionClassService dlPositionClassService;

	@GetMapping()
	@RequiresPermissions("system:dlPositionSubclass:dlPositionSubclass")
	String DlPositionSubclass(){
	    return "system/dlPositionSubclass/dlPositionSubclass";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:dlPositionSubclass:dlPositionSubclass")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<DlPositionSubclassDO> dlPositionSubclassList = dlPositionSubclassService.list(query);
		int total = dlPositionSubclassService.count(query);
		PageUtils pageUtils = new PageUtils(dlPositionSubclassList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:dlPositionSubclass:add")
	String add(Model model){
		List<DlPositionClassDO> list = dlPositionClassService.list(new HashMap<>());
		model.addAttribute("list", list);
		return "system/dlPositionSubclass/add";
	}

	@GetMapping("/edit/{positionSubclassId}")
	@RequiresPermissions("system:dlPositionSubclass:edit")
	String edit(@PathVariable("positionSubclassId") Integer positionSubclassId,Model model){
		DlPositionSubclassDO dlPositionSubclass = dlPositionSubclassService.get(positionSubclassId);
		List<DlPositionClassDO> list = dlPositionClassService.list(new HashMap<>());
		model.addAttribute("list", list);
		model.addAttribute("dlPositionSubclass", dlPositionSubclass);
	    return "system/dlPositionSubclass/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:dlPositionSubclass:add")
	public R save( DlPositionSubclassDO dlPositionSubclass){
		if(dlPositionSubclassService.save(dlPositionSubclass)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:dlPositionSubclass:edit")
	public R update( DlPositionSubclassDO dlPositionSubclass){
		dlPositionSubclassService.update(dlPositionSubclass);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:dlPositionSubclass:remove")
	public R remove( Integer positionSubclassId){
		try{
			if(dlPositionSubclassService.remove(positionSubclassId)>0){
				return R.ok();
			}
		} catch (Exception e){
			e.printStackTrace();
			return R.error("删除失败，请先删除其下面的职位");
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:dlPositionSubclass:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] positionSubclassIds){
		dlPositionSubclassService.batchRemove(positionSubclassIds);
		return R.ok();
	}
	
}
